﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WebService;
using WebService.IServices;
using WebService.Model;

namespace HX_WebService_Core.Controllers
{
    [Route("api/Menu")]
    [ApiController]
    //[Authorize]
    public class MenuController : ControllerBase
    {
        private IMenuServices menu;
        public MenuController(IMenuServices _menu)
        {
            this.menu = _menu;
        }

        [HttpPost("FindTree")]
        public List<Sys_Menu> FindTree([FromBody] Sys_User users)
        {
            IActionResult response = Unauthorized();
            List<Sys_Menu> menus = menu.FindByUser(users.Name).Result;
            List<Sys_Menu> sys_Menu = new List<Sys_Menu>();
            // 添加主菜单
            sys_Menu.AddRange(menus.Where(o => o.parent_id.Equals(0)));
            // 添加子菜单
            sys_Menu.ForEach(item =>
            {
                item.Children.AddRange(menus.Where(o => o.parent_id.Equals(item.id)));
            });
            return sys_Menu;
        }


        [HttpPost("FindPermissions")]
        public IActionResult FindPermissions([FromBody] Sys_User users)
        {
            IActionResult response = Unauthorized();
            //var user = loginServices.Query(d => d.Name == users.Name && d.Password == users.Password).Result;
            var message = new MessageModel<Sys_User>();
            //if (user != null && user.Count > 0)
            //{
            //var tokenString = BuildToken(user.First());
            message.Msg = null;
            message.Code = 200;
            message.Data = new string[] {
                null,
            "sys:user:view",
            "sys:menu:delete",
            "sys:dept:edit",
            "sys:dict:edit",
            "sys:dict:delete",
            "sys:menu:add",
            "sys:user:add",
            "sys:log:view",
            "sys:dept:delete",
            "sys:role:edit",
            "sys:role:view",
            "sys:dict:view",
            "sys:user:edit",
            "sys:user:delete",
            "sys:dept:view",
            "sys:dept:add",
            "sys:role:delete",
            "sys:menu:view",
            "sys:menu:edit",
            "sys:dict:add",
            "sys:role:add"};
            response = Ok(new
            {
                Message = message,
            });

            return response;
        }


    }
}